package com.java.cloud.order.service.impl;

import com.java.cloud.order.feign.AccountFeign;
import com.java.cloud.order.feign.StorageFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;

import com.java.cloud.order.dao.OrderDao;
import com.java.cloud.order.entity.Order;
import com.java.cloud.order.service.OrderService;

@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
	@Autowired
	private StorageFeign storageFeign;
	@Autowired
	private AccountFeign accountFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Order> page = this.page(
                new Query<Order>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

	@Override
	@GlobalTransactional
	public void create(Order order) {
		//开始创建新订单
		this.baseMapper.insert(order);
		//调用库存微服务扣减库存
		storageFeign.decrease(order.getProductId(),order.getCount());
		//调用账户微服务扣减余额
		accountFeign.decrease(order.getUserId(),order.getMoney());
		//修改订单状态
		order.setStatus(1);
		this.baseMapper.updateById(order);

		//上述代码只是演示分布式事务,实际项目应该用MQ解耦
	}

}